package jehc.djshi.workflow.web;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.base.BaseSearch;
import jehc.djshi.common.entity.OauthAccountEntity;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.workflow.model.LcExp;
import jehc.djshi.workflow.service.LcExpService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 流程表达式
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Api(value = "流程表达式", description = "流程表达式",tags = "流程表达式")
@RestController
@RequestMapping("/lcExp")
public class LcExpController extends BaseAction {

    @Resource
    LcExpService lcExpService;

    /**
     * 查询列表
     * @param baseSearch
     */
    @ApiOperation(value="查询列表", notes="查询列表")
    @PostMapping(value="/list")
    @Auth(value = "/lcExp/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<LcExp>> getLcExpListByCondition(@RequestBody BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        commonHPager(baseSearch);
        List<LcExp> lcExps = lcExpService.getLcExpListByCondition(condition);
        for(LcExp lcExp : lcExps){
            if(!StringUtil.isEmpty(lcExp.getCreateId())){
                OauthAccountEntity createBy = getAccount(lcExp.getCreateId());
                if(null != createBy){
                    lcExp.setCreateBy(createBy.getName());
                }
            }
            if(!StringUtil.isEmpty(lcExp.getUpdateId())){
                OauthAccountEntity modifieBy = getAccount(lcExp.getUpdateId());
                if(null != modifieBy){
                    lcExp.setModifiedBy(modifieBy.getName());
                }
            }
        }
        PageInfo<LcExp> page = new PageInfo<LcExp>(lcExps);
        return outPageBootStr(page,baseSearch);
    }

    /**
     * 查询单个对象
     * @param id
     */
    @ApiOperation(value="查询单个对象", notes="查询单个对象")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/lcExp/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<LcExp> getLcExpById(@PathVariable("id")String id){
        LcExp lcExp = lcExpService.getLcExpById(id);
        if(!StringUtil.isEmpty(lcExp.getCreateId())){
            OauthAccountEntity createBy = getAccount(lcExp.getCreateId());
            if(null != createBy){
                lcExp.setCreateBy(createBy.getName());
            }
        }
        if(!StringUtil.isEmpty(lcExp.getUpdateId())){
            OauthAccountEntity modifiedBy = getAccount(lcExp.getUpdateId());
            if(null != modifiedBy){
                lcExp.setModifiedBy(modifiedBy.getName());
            }
        }
        return outDataStr(lcExp);
    }

    /**
     * 添加
     * @param lcExp
     */
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value="/add")
    public BaseResult addLcExp(@RequestBody LcExp lcExp){
        int i = 0;
        if(null != lcExp){
            i=lcExpService.addLcExp(lcExp);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 修改
     * @param lcExp
     */
    @ApiOperation(value="修改", notes="修改")
    @PutMapping(value="/update")
    public BaseResult updateLcExp(@RequestBody LcExp lcExp){
        int i = 0;
        if(null != lcExp){
            i=lcExpService.updateLcExp(lcExp);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 删除
     * @param id
     */
    @ApiOperation(value="删除", notes="删除")
    @DeleteMapping(value="/delete")
    public BaseResult delLcExp(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=lcExpService.delLcExp(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }
}
